﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Models;
using BLL;
using MyVideo;

namespace LibraryManagerPro
{
    public partial class FrmBookManage : Form
    {

        private BookManager objBookManager = new BookManager();
        private List<Book> listBook = null;
        private Video objVideo = null;
        public FrmBookManage()
        {
            InitializeComponent();
            List<Category> list = objBookManager.GetAllCategory();
            list.Insert(0, new Category() { CategoryId = -1, CategoryName = "" });
            this.cboCategory.DataSource = list;
            this.cboCategory.DisplayMember = "CategoryName";
            this.cboCategory.ValueMember = "CategoryId";
            this.cboCategory.SelectedIndex = -1;

            this.btnDel.Enabled = false;
            this.btnSave.Enabled = false;
            this.dgvBookList.AutoGenerateColumns = false;

            this.cbo_BookCategory.DataSource = objBookManager.GetAllCategory();
            this.cbo_BookCategory.DisplayMember = "CategoryName";
            this.cbo_BookCategory.ValueMember = "CategoryId";
            this.cbo_BookCategory.SelectedIndex = -1;

            this.cbo_Publisher.DataSource = objBookManager.GetAllPublisher();
            this.cbo_Publisher.DisplayMember = "PublisherName";
            this.cbo_Publisher.ValueMember = "PublisherId";
            this.cbo_Publisher.SelectedIndex = -1;
        }


        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //首先断开选择改变事件
            this.dgvBookList.SelectionChanged -= new EventHandler(this.dgvBookList_SelectionChanged);
            if (this.cboCategory.SelectedValue.ToString() == "-1" && this.txtBarCode.Text.Trim().Length == 0 && this.txtBookName.Text.Trim().Length == 0)
            {
                MessageBox.Show("请至少选择一个查询条件", "查询提示");
            }
            else
            {
                listBook = objBookManager.GetBooks(Convert.ToInt32(this.cboCategory.SelectedValue), this.txtBarCode.Text.Trim().ToString(), this.txtBookName.Text.Trim().ToString());
                this.dgvBookList.DataSource = listBook;
                if(listBook.Count==0)
                {
                    this.btnDel.Enabled = false;
                    this.btnSave.Enabled = false;
                }
                else
                {
                    this.btnDel.Enabled = true;
                    this.btnSave.Enabled = true;
                }
            }
            //开启选择改变事件
            this.dgvBookList.SelectionChanged += new EventHandler(this.dgvBookList_SelectionChanged);
            dgvBookList_SelectionChanged(null, null);
        }
        #region 同步显示要修改的信息
        private void dgvBookList_SelectionChanged(object sender, EventArgs e)
        {
            if (this.dgvBookList.RowCount == 0) return;
            string barCode = this.dgvBookList.CurrentRow.Cells["BarCode"].Value.ToString();
            Book objBook = (from b in listBook where b.BarCode.Equals(barCode) select b).First<Book>();
            this.lbl_BarCode.Text = objBook.BarCode;
            this.txt_Author.Text = objBook.Author;
            this.lbl_BookCount.Text = objBook.BookCount.ToString();
            this.txt_BookName.Text = objBook.BookName;
            this.txt_BookPosition.Text = objBook.BookPosition;
            this.txt_UnitPrice.Text = objBook.UnitPrice.ToString();
            this.cbo_BookCategory.SelectedValue = objBook.BookCategory;
            this.cbo_Publisher.SelectedValue = objBook.PublisherId;
            this.lbl_BookId.Text = objBook.BoolId.ToString();
            if(objBook.BookImage.Length!=0)
            {
                this.pbCurrentImage.Image = (Image)new Common.SerializeObjectToString().DeserializeObject(objBook.BookImage);
            }
            else
            {
                this.pbCurrentImage.Image = null;
            }
        }
        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {

        }
    }
}
